﻿using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using zyconfig;

namespace zymain
{
    public partial class FrmCgysXk : Form
    {
        private int _kjm;
        private List<int> XnkList;
        private List<string> KsList;
        public int xnkId=1;
        public string gcd;
        public FrmCgysXk()
        {
            InitializeComponent();
        }
        public void AddConfig(int kjm)
        {
            this._kjm = kjm;
            this.AddDbList();
            this.AddKsList(this.xnkId);
        }
        private void AddDbList()
        {
            this.XnkList = new List<int>();
            this.XnkList.Clear();
            this.xnkKay.Items.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "select 库室名称,虚拟库室 from 虚拟库室 where  虚拟库室>0 order by 配置优先级";
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        this.xnkKay.Items.Add(dRow["库室名称"].ToString().Trim());
                        this.XnkList.Add(int.Parse(dRow["虚拟库室"].ToString()));
                    }
                }
                if (this.xnkKay.Items.Count > 0)
                {
                    this.xnkKay.SelectedIndex = 0;
                    this.xnkId = int.Parse(this.XnkList[0].ToString());
                }
            }
        }

        private void AddKsList(int xnkid)
        {
            this.KsList = new List<string>();
            this.KsList.Clear();
            this.gcdKay.Items.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "库键码=" + this._kjm;
                string kfks=_open.GetField("书目数据库定义", "可用分配库室").ToString().Trim();
                string[] kfs = kfks.Split(' ');
                string kss = "";
                foreach(string str in kfs)
                {
                    kss += "'" + str + "',";
                }
                string sql = " select 馆藏地址,部门名称 from 馆藏地址定义 where 虚拟库室=" + xnkid;
                if (!string.IsNullOrEmpty(kss))
                {
                    kss = kss.Substring(0, kss.Length - 1);
                    sql += " and 馆藏地址 in(" + kss + ") ";
                }
                sql += " order by 序号";
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        this.gcdKay.Items.Add(dRow["部门名称"].ToString().Trim());
                        this.KsList.Add(dRow["馆藏地址"].ToString());
                    }
                }
                if (this.gcdKay.Items.Count > 0)
                {
                    this.gcdKay.SelectedIndex = 0;
                    this.gcd = this.KsList[0].ToString().Trim();
                }
            }
        }

        private void xnk_SelextedIndexCheanged(object sender, System.EventArgs e)
        {
            int index = this.xnkKay.SelectedIndex;
            this.xnkId = this.XnkList[index];
            this.AddKsList(this.xnkId);
        }

        private void saveBtn_Click(object sender, System.EventArgs e)
        {
            DialogResult = DialogResult.OK;
            base.Close();
        }

        private void ks_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int index = this.gcdKay.SelectedIndex;
            this.gcd = this.KsList[index];
        }

        private void exitBtn_Click(object sender, System.EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            base.Close();
        }
    }
}
